package com.nit.lab.service.basic.impl;

import com.nit.lab.dto.basic.TeacherDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.basic.TeacherEntity;
import com.nit.lab.mapper.basic.TeacherMapper;
import com.nit.lab.service.basic.TeacherService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @文件名：TeacherServiceImpl
 * @创建人: wangsheng
 * @创建时间: 2020/3/14
 * @描述:
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired(required = false)
    private TeacherMapper teacherMapper;
    /**
     *@描述 获取数据表格
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = teacherMapper.count4Table(
                tableReqDTO.parseQueryParam("teacherCode"),
                tableReqDTO.parseQueryParam("teacherName"),
                tableReqDTO.parseQueryParam("teacherPhone"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                teacherMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("teacherCode"),
                        tableReqDTO.parseQueryParam("teacherName"),
                        tableReqDTO.parseQueryParam("teacherPhone"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }
    /**
     *@描述 添加教师信息
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(TeacherDTO teacherDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        TeacherEntity teacherEntity = mapperFactory.getMapperFacade().map(teacherDTO, TeacherEntity.class);
        teacherEntity.setTeacherPwd(DigestUtils.md5DigestAsHex("lab205@nit".getBytes()));
        teacherEntity.setGmtCreate(new Date());
        return teacherMapper.add(teacherEntity);
    }
    /**
     *@描述 验证TeacherCode的唯一性
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @Override
    public TeacherDTO loginCheck(String teacherCode) throws Exception {
        return teacherMapper.getByLoginName(teacherCode);
    }
    /**
     *@描述 编辑教师信息
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(TeacherDTO teacherDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        TeacherEntity teacherEntity = mapperFactory.getMapperFacade().map(teacherDTO, TeacherEntity.class);
        return teacherMapper.update(teacherEntity);
    }
    /**
     *@描述 删除教师信息
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String teacherIds) throws Exception {
        String[] arrayTeacherId = teacherIds.split(",");
        for (int i = 0; i < arrayTeacherId.length; i++) {
            teacherMapper.remove(Long.parseLong(arrayTeacherId[i]));
        }
        return arrayTeacherId.length;
    }
    /**
     *@描述 更改密码
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer resetPwd(Long teacherId, String teacherPwd) throws Exception {
        return teacherMapper.resetPwd(teacherId, DigestUtils.md5DigestAsHex(teacherPwd.getBytes()));
    }
    /**
     * @Description: 根据Id获取信息
     * @Author: lxr
     * @Date: 2020/03/25
     */
    @Override
    public TeacherDTO getByTeacherId(Long teacherId) throws Exception {
        return teacherMapper.getByTeacherId(teacherId);
    }

    @Override
    public List<TeacherDTO> listTeacher() throws Exception{
        return teacherMapper.listTeacher();
    }
}
